/* 赛事 */
var mt = new Vue ({
	el: "#match",
	data: {
		count: 0,
		count2: 0,
		
		mtId: "",
		mtTitle: "",
		mtMessage: "",
		refereeId: "",
		areaBookId: "",
		matchList: [],
		
		rfName: "",
		rfMessage: "",
		refereeList: [],
	
		areaId: "",
		areaList: [],
		
		registerId: 1,
	},
	created: function() {
		$.ajax({
			type: "GET",
			url: "/matchInfo/list",
			contentType: "application/json;charset=UTF-8",
			success: function(data){
				if(data.code == 200){
					var list = [];
					for(i = 0 ; i < data.data.length ; i++){
						var listData = {
							mtId: data.data[i].matchId,
							title: data.data[i].title,
							message: data.data[i].message,
							refereeId: data.data[i].refereeId,
							areaId: data.data[i].areaBookId,
							registerId: data.data[i].registerId,
						}
						list.push(listData);
					}
					mt.$data.matchList = list;
				}
			}
		});
		$.ajax({
			type: "GET",
			url: "/refereeInfo/list",
			contentType: "application/json;charset=UTF-8",
			success: function(data){
				if(data.code == 200){
					var list = data.data;
					mt.$data.refereeList = list;
				}
			}
		});
		$.ajax({
			type: "GET",
			url: "/areaInfo/list",
			contentType: "application/json;charset=UTF-8",
			success: function(data){
				if(data.code == 200){
					var list = data.data;
					mt.$data.areaList = list;
				}
			}
		});
	},
	methods: {
		createMatch: function(){
			var num = this.matchList.length - 1;
			var nextRfId;
			if(num >= 5){
				nextMtId = this.matchList[num].mtId + 1;
			}
			else{
				nextMtId = 0;
			}
			var addMatch = {
				mtId: nextMtId,
				title: this.mtTitle,
				message: this.mtMessage,
				refereeId: Number(this.refereeId),
				areaId: Number(this.areaId),
				registerId: this.registerId,
			}
			var data = {
				refereeId: addMatch.refereeId,
				areaBookId: addMatch.areaId,
				title: addMatch.title,
				message: addMatch.message,
			}
			var url = "/matchInfo";
			// var url = "/matchInfo";
			var lists = this.matchList;
			$.ajax({
				type: "POST",
				url: url,
				dataType: "json",
				contentType:"application/x-www-form-urlencoded;charset=utf-8",
				data: data,
				success: function(data){
					if(data.msg == "成功"){
						lists.push(addMatch);
					}
				}
			});
			this.mtTitle = this.mtMessage = "";
			this.areaBookId = this.refereeId = "";
		},
		delMatch: function(mtId){
			this.count = 0;
			var data = JSON.stringify(this.matchList[mtId].mtId);
			var url = "/matchInfo/" + data;
			// var url = "/matchInfo/" + data;
			var lists = this.matchList;
			$.ajax({
				type: "DELETE",
				url: url,
				dataType: "json",
				contentType:"application/x-www-form-urlencoded;charset=utf-8",
				data: data,
				success: function(data){
					if(data.msg == "成功"){
						// console.log(data);
						lists.splice(mtId, 1);
					}
				}
			});
		},
		getMtId: function(mtId){
			this.count = mtId;
			this.mtTitle = this.matchList[mtId].title;
			this.mtMessage = this.matchList[mtId].message;
			this.refereeId = this.matchList[mtId].refereeId;
			this.areaId = this.matchList[mtId].areaId;
		},
		updateMatch: function(){
			var list = {
				matchId: this.matchList[this.count].mtId,
				refereeId: refereeId = this.refereeId,
				areaBookId: this.areaId,
				title: this.mtTitle,
				message: this.mtMessage
			}
			var url = "/matchInfo";
			// var url = "/matchInfo";
			var lists = this.matchList;
			var count = this.count;
			$.ajax({
				type: "PUT",
				url: url,
				dataType: "json",
				contentType:"application/x-www-form-urlencoded;charset=utf-8",
				data: list,
				success: function(data){
					if(data.msg == "成功"){
						// console.log(count);
						lists[count].title = list.title;
						lists[count].message = list.message;
						lists[count].refereeId = list.refereeId;
						lists[count].areaId = list.areaBookId;
					}
				}
			});
		},
		clearData: function(){
			this.mtTitle = this.mtMessage = "";
			this.areaId = this.refereeId = "";
			this.rfName = this.rfMessage = "";
		},
		
		addReferee: function(){
			var num = this.refereeList.length - 1;
			var nextRfId;
			if(num >= 0){
				nextRfId = this.refereeList[num].refereeId + 1;
			}
			else{
				nextRfId = 0;
			}
			var addReferee = {
				refereeId: nextRfId,
				name: this.rfName,
				message: this.rfMessage,
			}
			var url = "/refereeInfo";
			// var url = "/refereeInfo";
			var lists = this.refereeList;
			$.ajax({
				type: "POST",
				url: url,
				dataType: "json",
				contentType:"application/x-www-form-urlencoded;charset=utf-8",
				data: addReferee,
				success: function(data){
					lists.push(addReferee);
					this.rfName = this.rfMessage = "";
				}
			});
			
		},
		delReferee: function(rfId){
			this.count2 = 0;
			var data = JSON.stringify(this.refereeList[rfId].refereeId);
			var url = "/refereeInfo/" + data;
			// var url = "/refereeInfo/" + data;
			var lists = this.refereeList;
			$.ajax({
				type: "DELETE",
				url: url,
				dataType: "json",
				contentType:"application/x-www-form-urlencoded;charset=utf-8",
				data: data,
				success: function(data){
					lists.splice(rfId, 1);
				}
			});
		},
		getRfId: function(rfId){
			this.count2 = rfId;
			this.rfName = this.refereeList[rfId].name;
			this.rfMessage = this.refereeList[rfId].message;
		},
		updateReferee: function(){
			var list = {
				refereeId: this.refereeList[this.count2].refereeId,
				name: this.rfName,
				message: this.rfMessage
			}
			var url = "/refereeInfo";
			// var url = "/refereeInfo";
			var lists = this.refereeList;
			var count = this.count2;
			$.ajax({
				type: "PUT",
				url: url,
				dataType: "json",
				contentType:"application/x-www-form-urlencoded;charset=utf-8",
				data: list,
				success: function(data){
					lists[count].name = list.name;
					lists[count].message = list.message;
				}
			});
		},
	}
});
